﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowDeletePerformance.xaml
    /// </summary>
    public partial class WindowDeletePerformance : Window
    {
        public WindowDeletePerformance()
        {
            InitializeComponent();
            lblMessage.Content = String.Empty;
            InitializeComboBoxRecorders();
        }

        private void InitializeComboBoxRecorders()
        {
            List<Recorder> recorders = Repository.GetAllRecorders();
            comboBoxRecorders.ItemsSource = recorders;
            comboBoxRecorders.SelectedValuePath = "ID";
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                lblMessage.Content = String.Empty;

                DateTime from = (DateTime)datePicker1.SelectedDate;
                DateTime to = (DateTime)datePicker2.SelectedDate;

                if (from == null) throw new ApplicationException("Start time have to be choosen.");
                if (to == null) throw new ApplicationException("End time have to be choosen.");
                if (to < from) throw new ApplicationException("Start time have to be lower than end time.");

                Recorder recorder = (Recorder)comboBoxRecorders.SelectedItem;

                if (recorder == null) throw new ApplicationException("Recorder should be chosen.");

                MessageBoxResult mbr = MessageBox.Show("Are you sure that you want to remove performance?", "Sure?", MessageBoxButton.YesNo);

                if (mbr == MessageBoxResult.No)
                {
                    lblMessage.Content = "Action was canceled.";
                    lblMessage.Foreground = Brushes.Blue;
                    return;
                }

                int count = 0;

                foreach (Line l in recorder.Lines)
                {
                    count += Repository.DeletePerformance(l.id, from, to);
                }

                lblMessage.Content = "Performance deleted successfully ( " + count + " rows).";
                lblMessage.Foreground = Brushes.Green;

            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }


    }
}
